home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 8: LINUX Games / Linux Cubed Series 8 - LINUX Games.iso / games / video / zapem-0.000 / zapem-0 / zapem / starfield.cc < prev    next >
C/C++ Source or Header  |  1995-06-03  |  2KB  |  104 lines

  1. /*    Copyright Alex Hornby 1994/1995. All rights reserved.
  2.      See file README for details
  3. */
  4.  
  5. /* 2D starfield */
  6.  
  7. #include "starfield.h"
  8. #include <iostream.h>
  9. #include "rnd.h"
  10. #include "btypes.h"
  11. #include "palette.h"
  12.  
  13. extern Palette gamepal;
  14.  
  15. StarField::StarField() : Screen()
  16. {
  17.     for(int f=0; f<NUMFIELD; f++)
  18.     {
  19.         for(int s=0; s<NUMSTAR; s++)
  20.         {
  21.             pos[s][f].x=rnd(getScreenWidth());
  22.             pos[s][f].y=rnd(getScreenHeight());
  23.             speed[s][f]=rnd(MAXSPEED)+1;
  24.         }
  25.     }
  26.     gamepal.allocate(0,0,0,0);
  27.     for(int i=0;i<NUMFIELD;i++)
  28.         color[i]=gamepal.request(48-6*i,48-6*i,48-6*i);
  29. }
  30.  
  31. void 
  32. StarField::getBack(void)
  33. {
  34. pixel *video;
  35.     for(int f=0; f<NUMFIELD; f++)
  36.     {
  37.         for(int s=0; s<NUMSTAR; s++)
  38.         {
  39.         video= getGraphMem() + pos[s][f].x + pos[s][f].y * getScreenWidth();
  40.         back[s][f]=*video;
  41.         }
  42.     }
  43. }
  44.  
  45. void
  46. StarField::replace(void)
  47. {
  48.     pixel *video;
  49.     for(int f=0; f<NUMFIELD; f++)
  50.     {
  51.         for(int s=0; s<NUMSTAR; s++)
  52.         {
  53.         video= getGraphMem() + pos[s][f].x + pos[s][f].y * getScreenWidth();
  54.         *video=back[s][f];
  55.         }
  56.     }
  57. }
  58.  
  59. void
  60. StarField::paste(void)
  61. {
  62.     pixel *video;
  63.     for(int f=0; f<NUMFIELD; f++)
  64.     {
  65.         for(int s=0; s<NUMSTAR; s++)
  66.         {
  67.         video= getGraphMem() + pos[s][f].x + pos[s][f].y * getScreenWidth();
  68.         *video=color[f];
  69.         }
  70.     }
  71. }
  72.  
  73. void
  74. StarField::update(void)
  75. {
  76.     replace();
  77.     move();
  78.     getBack();
  79.     paste();
  80. }
  81.  
  82. void
  83. StarField::move(void)
  84. {
  85.     static int time;
  86.     for(int f=0; f<NUMFIELD; f++)
  87.     {
  88.         if(time%(f+1)==0)
  89.         {
  90.             for(int s=0; s<NUMSTAR; s++)
  91.             {
  92.                 pos[s][f].y+=speed[s][f];
  93.                 if(pos[s][f].y > getScreenHeight())
  94.                 {
  95.                     pos[s][f].x=rnd(getScreenWidth());
  96.                     pos[s][f].y=0;
  97.                     speed[s][f]=rnd(MAXSPEED)+1;
  98.                 }
  99.             }
  100.         }
  101.     }
  102.     time++;
  103. }
  104.